package com.iman.Confucius.web.student;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.iman.Confucius.dto.RemarkStudyTimeDto;
import com.iman.Confucius.mappers.models.DrmCourse;
import com.iman.Confucius.service.student.CourseService;
import com.iman.Confucius.vo.CategReferencesVo;
import com.iman.Confucius.vo.CommentVo;
import com.iman.Confucius.vo.CourseClassmateVo;
import com.iman.Confucius.vo.CourseCtgVo;
import com.iman.Confucius.vo.CourseNoteVo;
import com.iman.Confucius.vo.CourseTeacherVo;
import com.iman.Confucius.vo.CourseVo;
import com.iman.Confucius.vo.HomeworkVo;
import com.iman.Confucius.vo.ResourceVo;
import com.iman.Confucius.web.BaseController;
import com.iman.common.Constants;
import com.iman.common.support.Page;
import com.iman.common.util.ConfigUtil;
import com.iman.common.util.DateUtils;
import com.iman.common.util.EncryptTool;
import com.iman.common.util.IdCreater;
import com.iman.common.util.JsonMapper;
import com.iman.common.video.QNAuth;


/**
 * *************************************************************************
 * @文件名称: MyCurriculumController.java
 *
 * @包路径  : com.iman.Confucius.web.student 
 *				 
 * @版权所有:   TSW   科技有限公司 (C) 2014
 *
 * @类描述:   我的课程
 * 
 * @创建人:   huangt  
 *
 * @创建时间: 2014年12月23日 - 下午3:43:57 
 *
 * @修改记录:
   -----------------------------------------------------------------------------------------------
             时间						|		修改人		|		修改的方法		|		修改描述                                                                
   -----------------------------------------------------------------------------------------------
							|					|					|                                       
   ----------------------------------------------------------------------------------------------- 	
 
 **************************************************************************
 */

@Controller
@RequestMapping(value="/student/curriculum/*")
public class MyCurriculumController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(MyCurriculumController.class);

    
    
	@Autowired
	private CourseService courseService;
	
	private static enum COURSE_SUB {
		ALL(1), TOPIC(2), VOD(3), FACE(4);
		private int tabId = 0;
		COURSE_SUB(int tabId) {
			this.tabId = tabId;
		}
	}
	
//	private static enum COURSE_TYPE {
//		FACE("面授");
//		private String type;
//		COURSE_TYPE(String type) {
//			this.type = type;
//		}
//	}
	
	/**
	 * 
	 *  Function:
	 *  功能说明：初始化我的课程页面
	 *	 使用说明：
	 *  @author  huangt  DateTime 2014年12月22日 下午3:45:30
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @param menu
	 *  @return
	 * @throws Exception  
	 */
	@RequestMapping(value="/init")
	public ModelAndView initMyCurriculum(HttpServletRequest request) throws Exception {
		return initAllCurri(request, "0", "4");
	} 
	
	/**
	 * 
	 *  Function:
	 *  功能说明：初始化某个课程的课件页面
	 *	 使用说明：
	 *  @author  huangt  DateTime 2014年12月22日 下午3:45:30
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @param menu
	 *  @return
	 * @throws Exception  
	 */
	@RequestMapping(value="/initCourseCtg/{courseId}")
	public ModelAndView initCourseCtg(HttpServletRequest request,
			@Valid @PathVariable String courseId) throws Exception {
		return initCourseCtgList(request, courseId, "0", "5");
	}
	
	
	@RequestMapping(value = "/commentStar", method = RequestMethod.GET)
	@ResponseBody
	public String commentStar(HttpServletResponse res, HttpServletRequest req){
		String fen = req.getParameter("fen");
		return fen;
	}
	
	
	/**
	 * @description 初始化专题课程列表页面
	 * @param request
	 * @param courseId
	 * 			专题课程ID
	 * @return ModelAndView
	 * @throws Exception 
	 * @author huangt  2014年12月26日
	 */
	@RequestMapping(value="/initCourseTopic/{courseId}")
	public ModelAndView initCourseTopic(HttpServletRequest request,
			@Valid @PathVariable String courseId) throws Exception {
		return initTopicCourse(request, courseId, "0", "5");
	}
	
	/**
	 * @description 获得所有课程列表
	 * @param request
	 * @param pageNo
	 * @param pageSize
	 * @return ModelAndView
	 * @throws Exception 
	 * @author huangt  2014年12月24日
	 */
	@RequestMapping(value="/allCurri/{pageNo}/{pageSize}")
	public  ModelAndView initAllCurri(HttpServletRequest request,
			@Valid @PathVariable  String pageNo, 
			@Valid @PathVariable String pageSize) throws Exception {
		if(!StringUtils.isNumeric(pageNo)){
			throw new IllegalArgumentException("无效分页参数");
		}
		if(!StringUtils.isNumeric(pageSize)){
			throw new IllegalArgumentException("无效分页参数");
		}
		Page<CourseVo> pageList = new Page<CourseVo>();
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录用户ID
		Long  userId = super.getUserIdInSession(request);
		map.put("userId", userId);
		map.put(Constants.PAGE_INDEX_KEY, Integer.parseInt(pageNo));
		map.put(Constants.PAGE_SIZE_KEY, Integer.parseInt(pageSize));
		pageList = courseService.getAllCourseList(map);
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("pageList", pageList);
		model.put("tabstatus", String.valueOf(COURSE_SUB.ALL.tabId));
		model.put("topicCount", pageList.getTotalCount());
		ModelAndView view =null;
		view = new ModelAndView("student/my_course",model);
		putResourceSrc(view);
		//动态返回 访问路径分页路径
		view.addObject("subPath", "allCurri");
		return view;
	}

	/**
	 * @description 获得点播（线上）课程列表
	 * @param request
	 * @param pageNo
	 * @param pageSize
	 * @return ModelAndView
	 * @author huangt  2014年12月29日
	 */
	@RequestMapping(value="/vodCurri/{pageNo}/{pageSize}")
	public  ModelAndView initVODCurri(HttpServletRequest request,
			@Valid @PathVariable String pageNo, 
			@Valid @PathVariable String pageSize) throws Exception {
		if(!StringUtils.isNumeric(pageNo)){
			throw new IllegalArgumentException("无效分页参数");
		}
		if(!StringUtils.isNumeric(pageSize)){
			throw new IllegalArgumentException("无效分页参数");
		}
		Page<CourseVo> pageList = new Page<CourseVo>();
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录用户ID
		Long  userId = super.getUserIdInSession(request);
		map.put("userId", userId);
		map.put(Constants.PAGE_INDEX_KEY, Integer.parseInt(pageNo));
		map.put(Constants.PAGE_SIZE_KEY, Integer.parseInt(pageSize));
		//设置课程为“线上”类型
		map.put("courseOnlinetp", 1);
		pageList = courseService.getAllCourseList(map);
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("pageList", pageList);
		model.put("tabstatus", String.valueOf(COURSE_SUB.VOD.tabId));
		model.put("topicCount", pageList.getTotalCount());
		ModelAndView view =null;
		view = new ModelAndView("student/my_course",model);
		putResourceSrc(view);
		
		//动态返回 访问路径分页路径
		view.addObject("subPath", "vodCurri");
		return view;
	}
	
	/**
	 * @description 获得专题课程列表
	 * @param request
	 * @param pageNo
	 * @param pageSize
	 * @return ModelAndView
	 * @throws Exception 
	 * @author huangt  2014年12月24日
	 */
	@RequestMapping(value="/topicCurri/{pageNo}/{pageSize}")
	public  ModelAndView initTopicCurri(HttpServletRequest request,
			@Valid @PathVariable String pageNo, 
			@Valid @PathVariable String pageSize) throws Exception {
		if(!StringUtils.isNumeric(pageNo)){
			throw new IllegalArgumentException("无效分页参数");
		}
		if(!StringUtils.isNumeric(pageSize)){
			throw new IllegalArgumentException("无效分页参数");
		}
		Page<CourseVo> pageList = new Page<CourseVo>();
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录用户ID
		Long  userId = super.getUserIdInSession(request);
		map.put("userId", userId);
		map.put("courseSubject", COURSE_SUB.TOPIC.tabId);
		map.put(Constants.PAGE_INDEX_KEY, Integer.parseInt(pageNo));
		map.put(Constants.PAGE_SIZE_KEY, Integer.parseInt(pageSize));
		pageList = courseService.getAllCourseList(map);
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("pageList", pageList);
		model.put("tabstatus", String.valueOf(COURSE_SUB.TOPIC.tabId));
		model.put("topicCount", pageList.getTotalCount());
		ModelAndView view =null;
		view = new ModelAndView("student/my_course",model);
		putResourceSrc(view);
		
		//动态返回 访问路径分页路径
		view.addObject("subPath", "topicCurri");
		return view;
	}
	
	/**
	 * @description 获得面授（线下）课程列表
	 * @param request
	 * @param pageNo
	 * @param pageSize
	 * @return ModelAndView
	 * @throws Exception 
	 * @author huangt  2014年12月25日
	 */
	@RequestMapping(value="/faceCurri/{pageNo}/{pageSize}")
	public  ModelAndView initFaceCurri(HttpServletRequest request,
			@Valid @PathVariable String pageNo, 
			@Valid @PathVariable String pageSize)  throws Exception {
		if(!StringUtils.isNumeric(pageNo)) {
			throw new IllegalArgumentException("无效分页参数");
		}
		if(!StringUtils.isNumeric(pageSize)){
			throw new IllegalArgumentException("无效分页参数");
		}
		Page<CourseVo> pageList = new Page<CourseVo>();
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录用户ID
		Long  userId = super.getUserIdInSession(request);
		map.put("userId", userId);
//		map.put("courseType", COURSE_TYPE.FACE.type);
		//设置课程为“线下”类型  huangt  2014-12-29
		map.put("courseOnlinetp", 2);
		map.put(Constants.PAGE_INDEX_KEY, Integer.parseInt(pageNo));
		map.put(Constants.PAGE_SIZE_KEY, Integer.parseInt(pageSize));
		pageList = courseService.getAllCourseList(map);
		
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("pageList", pageList);
		model.put("tabstatus", String.valueOf(COURSE_SUB.FACE.tabId));
		model.put("faceCount", pageList.getTotalCount());
		ModelAndView view =null;
		view = new ModelAndView("student/my_course",model);
		putResourceSrc(view);
		
		//动态返回 访问路径分页路径
		view.addObject("subPath", "faceCurri");
		return view;
	}
	

	/**
	 * 
	 *  Function:
	 *  功能说明：获取公开课的，外部点击公开的进入页面
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2015年11月18日 下午11:41:03
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @param courseId
	 *  @return
	 *  @throws Exception
	 */
	@RequestMapping(value="/pinitCourseCtg/{courseId}")
	public ModelAndView initCourseCtgList(HttpServletRequest request,
			@Valid @PathVariable String courseId ) throws Exception {
		ModelAndView view =null;
	 
		if(!StringUtils.isNumeric(courseId)){
			throw new IllegalArgumentException("无效课程ID");
		}
		Map<String, Object> model = new HashMap<String, Object>();
		Page<CourseCtgVo> pageList = new Page<CourseCtgVo>();
		//条件Map
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录用户ID
		Long  userId = super.getUserIdInSession(request);
		map.put("userId", userId);
		map.put("courseId", courseId);
		map.put(Constants.PAGE_INDEX_KEY, 0);
		map.put(Constants.PAGE_SIZE_KEY, 5);
		Page<CourseVo> courseList = courseService.getAllPublicCourseList(map);
		if (courseList.getTotalCount() > 0) {
			model.put("myCourse", courseList.getData().get(0));
			
			model.put("ispublic",true);
			
			//获得教师信息  huangt  2015-01-04
			long teacherId = courseList.getData().get(0).getCourseTeacherId();
			CourseTeacherVo teacher = courseService.getCourseTeacherById(teacherId);
			model.put("teacher", teacher);
			
			//获得课时列表  huangt  2014-12-26
			pageList = courseService.getCourseCtgList(map);
			model.put("pageList", pageList);
			//获得课程笔记列表
			Page<CourseNoteVo> noteList = new Page<CourseNoteVo>();
			noteList = courseService.getCourseNoteList(map);
			model.put("noteList", noteList);
			
			//获得课程评论列表  huangt  2014-12-29
			Page<CommentVo> commentList = new Page<CommentVo>();
			commentList = courseService.getCourseCommentList(map);
			model.put("commentList", commentList);
			model.put("commentSubList", commentList);
			
			//获得同学列表  huangt  2014-12-29
			Page<CourseClassmateVo> classmateList = new Page<CourseClassmateVo>();
			map.put(Constants.PAGE_SIZE_KEY, Integer.MAX_VALUE);
			classmateList = courseService.getCourseClassmateList(map);
			model.put("classmateList", classmateList);
		} else {
			model.put("error", "您尚未购买该课程！");
		}
		
		//获取推荐课程
		List<Map<String, String>> mapCourse = courseService.getCourseTop5();
		model.put("courseTop", mapCourse);
		
		 
		view = new ModelAndView("student/my_course_list",model);
		putResourceSrc(view);
		return view;
	}
	
	/**
	 * 
	 *  Function:
	 *  功能说明：内部学习平台点击进入页面
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2015年11月18日 下午11:45:24
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @param courseId
	 *  @param pageNo
	 *  @param pageSize
	 *  @return
	 *  @throws Exception
	 */
	@RequestMapping(value="/initCourseCtgList/{courseId}/{pageNo}/{pageSize}")
	public ModelAndView initCourseCtgList(HttpServletRequest request,
			@Valid @PathVariable String courseId,
			@Valid @PathVariable String pageNo, 
			@Valid @PathVariable String pageSize) throws Exception {
		ModelAndView view =null;
		if(!StringUtils.isNumeric(pageNo)){
			throw new IllegalArgumentException("无效分页参数");
		}
		if(!StringUtils.isNumeric(pageSize)){
			throw new IllegalArgumentException("无效分页参数");
		}
		if(!StringUtils.isNumeric(courseId)){
			throw new IllegalArgumentException("无效课程ID");
		}
		Map<String, Object> model = new HashMap<String, Object>();
		Page<CourseCtgVo> pageList = new Page<CourseCtgVo>();
		//条件Map
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录用户ID
		Long  userId = super.getUserIdInSession(request);
		map.put("userId", userId);
		map.put("courseId", courseId);
		map.put(Constants.PAGE_INDEX_KEY, Integer.parseInt(pageNo));
		map.put(Constants.PAGE_SIZE_KEY, Integer.parseInt(pageSize));
		Page<CourseVo> courseList = courseService.getAllCourseList(map);
		if (courseList.getTotalCount() > 0) {
			model.put("myCourse", courseList.getData().get(0));
			//获得教师信息  huangt  2015-01-04
			long teacherId = courseList.getData().get(0).getCourseTeacherId();
			CourseTeacherVo teacher = courseService.getCourseTeacherById(teacherId);
			model.put("teacher", teacher);
			
			//获得课时列表  huangt  2014-12-26
			pageList = courseService.getCourseCtgList(map);
			model.put("pageList", pageList);
			//获得课程笔记列表
			Page<CourseNoteVo> noteList = new Page<CourseNoteVo>();
			noteList = courseService.getCourseNoteList(map);
			model.put("noteList", noteList);
			
			//获得课程评论列表  huangt  2014-12-29
			Page<CommentVo> commentList = new Page<CommentVo>();
			map.put(Constants.PAGE_SIZE_KEY, Integer.MAX_VALUE);
			commentList = courseService.getCourseCommentList(map);
			model.put("commentList", commentList);
			model.put("commentSubList", commentList);
			
			//获得同学列表  huangt  2014-12-29
			Page<CourseClassmateVo> classmateList = new Page<CourseClassmateVo>();
			classmateList = courseService.getCourseClassmateList(map);
			model.put("classmateList", classmateList);
		} else {
			model.put("error", "您尚未购买该课程！");
		}
		
		//获取推荐课程
		List<Map<String, String>> mapCourse = courseService.getCourseTop5();
		model.put("courseTop", mapCourse);
		
		view = new ModelAndView("student/my_course_list",model);
		putResourceSrc(view);
		return view;
	}
	
	
	
	
	
	
	/**
	 * @description 获得课程课件（时）列表（右侧栏）
	 * @return ModelAndView
	 * @throws Exception 
	 * @author huangt  2014年12月31日
	 */
	@RequestMapping(value="/videoList/{courseId}/{ctgId}/{pageNo}/{pageSize}")
	public ModelAndView initCourseVideoList(HttpServletRequest request,
			@Valid @PathVariable String courseId,
			
			@Valid @PathVariable String  ctgId,
			@Valid @PathVariable String pageNo, 
			@Valid @PathVariable String pageSize,String ispublic) throws Exception {
		if(!StringUtils.isNumeric(pageNo)){
			throw new IllegalArgumentException("无效分页参数");
		}
		if(!StringUtils.isNumeric(pageSize)){
			throw new IllegalArgumentException("无效分页参数");
		}
		if(!StringUtils.isNumeric(courseId)){
			throw new IllegalArgumentException("无效课程ID");
		}
		Map<String, Object> model = new HashMap<String, Object>();
		Page<CourseCtgVo> pageList = new Page<CourseCtgVo>();
		//条件Map
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录用户ID
		Long  userId = super.getUserIdInSession(request);
		map.put("userId", userId);
		map.put("courseId", courseId);
		map.put(Constants.PAGE_INDEX_KEY, Integer.parseInt(pageNo));
		map.put(Constants.PAGE_SIZE_KEY, Integer.parseInt(pageSize));
//		Page<CourseVo> courseList = courseService.getAllCourseList(map);
//		model.put("myCourse", courseList.getData().get(0));
		//获得课时列表
		pageList = courseService.getCourseCtgList(map);
		model.put("pageList", pageList);
		model.put("currctgId", ctgId);
		//是不是公开课点击查看进入的
		model.put("ispublic", Boolean.valueOf( ispublic ));
		
//		model.put("tabstatus", String.valueOf(COURSE_SUB.NORMAL.tabId));
		ModelAndView view =null;
		view = new ModelAndView("student/my_course_videolist",model);
		putResourceSrc(view);
		return view;
	}
	
	/**
	 * @description 获得专题课程列表
	 * @param request
	 * @param courseId
	 * @param pageNo
	 * @param pageSize
	 * @return ModelAndView
	 * @throws Exception 
	 * @author huangt  2014年12月26日
	 */
	@RequestMapping(value="/initTopicCourse/{courseId}/{pageNo}/{pageSize}")
	public ModelAndView initTopicCourse(HttpServletRequest request,
			@Valid @PathVariable String courseId,
			@Valid @PathVariable String pageNo, 
			@Valid @PathVariable String pageSize) throws Exception {
		if(!StringUtils.isNumeric(pageNo)){
			throw new IllegalArgumentException("无效分页参数");
		}
		if(!StringUtils.isNumeric(pageSize)){
			throw new IllegalArgumentException("无效分页参数");
		}
		if(!StringUtils.isNumeric(courseId)){
			throw new IllegalArgumentException("无效课程ID");
		}
		Map<String, Object> model = new HashMap<String, Object>();
		//条件Map
		Map<String, Object> map = new HashMap<String, Object>();
		//获取登录用户ID
		Long  userId = super.getUserIdInSession(request);
		map.put("userId", userId);
		map.put("courseId", courseId);
		map.put(Constants.PAGE_INDEX_KEY, Integer.parseInt(pageNo));
		map.put(Constants.PAGE_SIZE_KEY, Integer.parseInt(pageSize));
		Page<CourseVo> courseList = courseService.getAllCourseList(map);
		model.put("myCourse", courseList.getData().get(0));
		
		//获得专题课程列表
		map.remove("courseId");
		map.put("subjectId", courseId);
		Page<CourseVo> topicList = new Page<CourseVo>();
		topicList = courseService.getTopicCourseList(map);
		model.put("topicList", topicList);
		ModelAndView view =null;
		view = new ModelAndView("student/my_course_topic",model);
		putResourceSrc(view);
		return view;
	}
	
	/**
	 * @description 初始化作业信息
	 * @param request
	 * @param homework
	 * @return ModelAndView
	 * @throws Exception 
	 * @author huangt  2014年12月30日
	 */
	@RequestMapping(value="/initHomework/{homeworkId}")
	public ModelAndView initHomework(HttpServletRequest request,
			@Valid @PathVariable String homeworkId) throws Exception {
		if(!StringUtils.isNumeric(homeworkId)){
			throw new IllegalArgumentException("无效的作业ID");
		}
		Map<String, Object> model = new HashMap<String, Object>();
		//条件Map
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Constants.PAGE_INDEX_KEY, 0);
		map.put(Constants.PAGE_SIZE_KEY, 1);
		map.put("homeworkId", homeworkId);
		Page<HomeworkVo> homework = courseService.getCtgHomework(map);
		model.put("homework", homework.getData().get(0));
		ModelAndView view =null;
		view = new ModelAndView("student/my_course_homework",model);
		putResourceSrc(view);
		return view;
	}
	
	
	
	/**
	 * 
	 *  Function:
	 *  功能说明：如果是公开课那么返回 公开课内容也没，不再验证
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2015年11月18日 下午11:16:50
	 *	返回类型: ModelAndView    
	 *  @param request
	 *  @param courseId
	 *  @param ctgId
	 *  @param srcId
	 *  @param isPublic
	 *  @return
	 *  @throws Exception
	 */
	@RequestMapping(value="/pinitVideo/{courseId}/{ctgId}/{srcId}")
	public ModelAndView pinitCourseVideo(HttpServletRequest request,
			@Valid @PathVariable String courseId,
			@Valid @PathVariable String ctgId,
			@Valid @PathVariable String srcId, Boolean isPublic) throws Exception {
		
		return this.initCourseVideo(  request,  courseId,  ctgId,  srcId,true);
	}
	/**
	 * @description 初始化课时视频播放页面
	 * @param request
	 * @param courseId
	 * @param id
	 * 			课件ID
	 * @param srcId
	 * 			资源ID
	 * @isPublic 是否是公开的过来看视频的
	 * @return ModelAndView
	 * @throws Exception 
	 * @author huangt  2014年12月31日
	 */
	@RequestMapping(value="/initVideo/{courseId}/{ctgId}/{srcId}")
	public ModelAndView initCourseVideo(HttpServletRequest request,
			@Valid @PathVariable String courseId,
			@Valid @PathVariable String ctgId,
			@Valid @PathVariable String srcId, boolean isPublic) throws Exception {
		if(!StringUtils.isNumeric(courseId)){
			throw new IllegalArgumentException("无效的课程ID");
		}
		if(!StringUtils.isNumeric(ctgId)){
			throw new IllegalArgumentException("无效的课件ID");
		}
		if(StringUtils.isEmpty(srcId)){
			throw new IllegalArgumentException("无效的资源ID");
		}
		Map<String, Object> model = new HashMap<String, Object>();
		//条件Map
		Map<String, Object> map = new HashMap<String, Object>();
		Page<CourseVo> courseList = new Page<CourseVo>();
		Page<ResourceVo> resList = new Page<ResourceVo>();
		
		
	
		
		Long  userId = super.getUserIdInSession(request);
		map.put("userId", userId);
		map.put("courseId", courseId);
		map.put(Constants.PAGE_INDEX_KEY, 0);
		map.put(Constants.PAGE_SIZE_KEY, 1);
		//isPublic 如果是公开课那么获取公开课内容 ， 如果不是那么还是之前的内容
		courseList =   isPublic==true? courseService.getAllPublicCourseList(map) :  courseService.getAllCourseList(map);
		CourseVo course = new CourseVo();
		
		ResourceVo video = new ResourceVo();
		//当前课件是否可以学习，判断之前是否还有没学完的吗
		boolean canStudy = true;
		//验证是否可以继续学习
		if (courseList.getData() != null) {
			course= courseList.getData().get(0);
			//进度  100%  
			 int  progress = course.getProgress();
			 
			//数据库表
			DrmCourse drmcourse = this.courseService.getCouserById(NumberUtils.toLong(courseId,0));
			Integer drmguocheng = drmcourse.getCourseFlowyn();
			
			 //是否过程话学习
 			if(drmguocheng!=null && drmguocheng.intValue() >0){
				//购买学习记录的  学习主键
				map.put("studyFkId", course.getStudyId());
				map.put("categoryId",ctgId);
				
				Integer  notstudy = this.courseService.canStudyNextCategory(map);
				//如果有没学习达到 80%的，那么 返回script的时候，不返回可以学习
				if(notstudy !=null && notstudy.intValue()>0){
					canStudy = false;
				}
			}
			
			if(  course.getCanStudy()==1) {
				//可以继续学习，返回资源实体
				map.put("srcId", srcId);
//				map.put("id", id);
				//当前点击的 课件学习；
				Page<ResourceVo> resource = courseService.getCtgResource(map);
				//所有的课件：
				
				//条件Map
				Map<String, Object> mapall = new HashMap<String, Object>();
				
				mapall.put(Constants.PAGE_INDEX_KEY,  0);
				mapall.put(Constants.PAGE_SIZE_KEY, Integer.MAX_VALUE);
				mapall.put("courseId", courseId);
				//查看到前端没有用到 所以注释；  2015年11月19日
				//resList = courseService.getCourseResList(mapall);
				
				if (resource.getData() != null && resource.getData().size() > 0) {
					video = resource.getData().get(0);
				}
			}
		}
		course.setIspublic( isPublic);//设置是否是公开课，默认不是
		model.put("course", course);
		model.put("video", video);
		model.put("resList", resList);
		model.put("currCtgId", ctgId);
		
		//控制是否有没学习完成的
		model.put("canStudy", canStudy);
		
	 
		String  safeKey =  EncryptTool.sha1HEX( courseId+ctgId+srcId+DateUtils.getCurrentStringDateYYMDHMSS() ) ;
		String  safeValue = safeKey+safeKey;
		//存入redis
		this.courseService.studyKey(safeKey, safeValue);
		model.put("safeKey", safeKey);
		
		if( "qiniu".equalsIgnoreCase(video.getResorceStocktp() )){
			//设置URL
			String videoPath = ConfigUtil.getString("qiniu.domain");
			QNAuth auth = new QNAuth();
			//有效期20s
			String url = auth.getVideoURL(videoPath+"/"+video.getName(),10);
			video.setVideoResourceUrl(url );
		}else if("url".equalsIgnoreCase(video.getResorceStocktp() )){
			//引入外部的 ＵＲＬ　　比如：http://7xllug.media1.z0.glb.clouddn.com/卯拳.mp4
			video.setVideoResourceUrl(video.getResouceFid() );
		}else{
			//设置URL
			String videoPath = ConfigUtil.getString("video.server");
			String videoResourceUrl = videoPath +"/"+ video.getResouceFid()  +"?r="+safeKey;
			video.setVideoResourceUrl(videoResourceUrl );
		}
		
		
		
		
		ModelAndView view =null;
		view = new ModelAndView("student/my_course_video",model);
		putResourceSrc(view);
		return view;
	}
	
	/**
	 * @description 获得课件资源列表
	 * @param request
	 * @param ctgId
	 * @param pageNo
	 * @param pageSize
	 * @return ModelAndView
	 * @throws Exception 
	 * @author huangt  2015年1月4日
	 */
	@RequestMapping(value="/videoRes/{courseId}/{pageNo}/{pageSize}")
	public ModelAndView initVideoRes(HttpServletRequest request,
			@Valid @PathVariable String courseId,
			@Valid @PathVariable  String pageNo, 
			@Valid @PathVariable String pageSize) throws Exception {
		if(!StringUtils.isNumeric(courseId)){
			throw new IllegalArgumentException("无效的课程ID");
		}
		Map<String, Object> model = new HashMap<String, Object>();
		//条件Map
		Map<String, Object> map = new HashMap<String, Object>();
		Page<CategReferencesVo> resList = new Page<CategReferencesVo>();
		map.put(Constants.PAGE_INDEX_KEY,  Integer.parseInt(pageNo));
		map.put(Constants.PAGE_SIZE_KEY,  Integer.parseInt(pageSize));
		map.put("courseId", courseId);
		resList = courseService.getCourseeferencesList(map);
	
		model.put("resList", resList);
		ModelAndView view =null;
		view = new ModelAndView("student/my_course_videodata",model);
		putResourceSrc(view);
		return view;
	}
	
	
	
	/**
	 * 
	 *  Function:
	 *  功能说明： 观看时间   更新观看时间，每次打开后更新
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2015年3月14日 下午8:09:27
	 *	返回类型: String    
	 *  @param request
	 *  @param courseId
	 *  @param time
	 *  @return
	 *  @throws Exception
	 */
	@RequestMapping(value="/p/{courseId}/{ctgId}/{safeKey}/{time}")
	@ResponseBody
	public Map studyProcess(HttpServletRequest request,
			@Valid @PathVariable String courseId,
			@Valid @PathVariable String ctgId	,
			@Valid @PathVariable  String time  	,
			@Valid @PathVariable String safeKey
		 ) throws Exception {
		
		Map result = new HashMap();
		
		if(!StringUtils.isNumeric(courseId)){
			result.put("code", -1);
			result.put("msg", "无效的课程ID");
			logger.info("设置内容 结果>>>>>>"+result);
			return result;
		}
		
		if(!StringUtils.isNumeric(time) ||"0".equals(time)){
			result.put("code", -1);
			result.put("msg", "无效的学习时间");
			logger.info("设置内容 结果>>>>>>"+result);
			return result;
		}
		
		//如果存在  在处理别的事情
		boolean  exsitKey = this.courseService.exsitStudyKey(safeKey );
		
		if(!exsitKey){
			result.put("code", -1);
			result.put("msg",  "safeKey不存在");
			logger.info("设置内容 结果>>>>>>"+result);
			return result;
		}
		
		
		Long userId = this.getUserIdInSession(request);
		
		logger.info("提交的内容>>>>>>userid=:"+userId+courseId+"ctgId="+ctgId+"time:="+time);
		
		long studyTime = NumberUtils.toLong(time);
		 
		RemarkStudyTimeDto dto = new RemarkStudyTimeDto();
		dto.setCourseId(courseId);
		dto.setCtgId(ctgId);
		dto.setSeconds(studyTime);
		dto.setUserId(userId);
		//TODO  记录学习时长
		 String value =  JsonMapper.toJson(dto);
		 String key	 = courseId+":"+ctgId+":"+userId;
		Map <String,String> study_remark  = new HashMap<String,String>();
		study_remark.put(key, value);
		
		logger.info("设置内容>>>>>>"+value);
	
		boolean set = this.courseService.setRemarkStudentStudyTime(study_remark);
	 
		if(set){
			result.put("code", 0);
			result.put("msg", "更新成功");
		}else{
			result.put("code", -1);
			result.put("msg", "更新失败");
		}
		logger.info("设置内容 结果>>>>>>"+result);
		
		return result  ;
	}
	
	
	/**
	 * 
	 *  Function:
	 *  功能说明：发布评论
	 *	 使用说明：
	 *  @author  wangxiaolei  DateTime 2015年3月14日 下午8:09:27
	 *	返回类型: String    
	 *  @param request
	 *  @param courseId
	 *  @return
	 *  @throws Exception
	 */
	@RequestMapping(value="/addComment/{courseId}/{pid}/{commontText}/{commontStar}")
	@ResponseBody
	public Map addComment(HttpServletRequest request,
			@Valid @PathVariable String courseId,
			@Valid @PathVariable String pid,
			@Valid @PathVariable String commontText,
			@Valid @PathVariable String commontStar
		 ) throws Exception {
		
		Map result = new HashMap();
		long userId = this.getUserIdInSession(request);
		CommentVo cv = new CommentVo();
		cv.setId(IdCreater.getId());
		cv.setUserId(userId+"");
		if(StringUtils.isNumeric(courseId)){
			cv.setCourseId(Long.parseLong(courseId));
		}
		cv.setCommontText(commontText);
		if(pid == null || "null".equals(pid) || "-1".equals(pid)){
			cv.setCommontPid((long) -1);
		}else {
			cv.setCommontPid(Long.parseLong(pid));
		}
		cv.setCommontSeq(1);
		cv.setCommentStar(Integer.parseInt(commontStar));
		boolean set =  this.courseService.addCourseComment(cv);
		if(set){
			result.put("code", 0);
			result.put("msg", "发布成功");
		}else{
			result.put("code", -1);
			result.put("msg", "发布失败");
		}
		logger.info("发布评论>>>>>>"+result);
		
		return result  ;
	}
	
}
